package com.vcit.StudentGrade.controller;


import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.vcit.system.domain.grade.TStudentGrade;
import com.vcit.system.domain.vo.StudentGradeVo;
import com.vcit.system.service.grade.ITStudentGradeService;
import com.vcit.system.service.ITClazzStudentService;
import com.vcit.system.service.grade.ITClazzTermCourseService;
import com.vcit.system.domain.clazz.TClazzStudent;
import com.vcit.system.domain.grade.TClazzTermCourse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.vcit.common.annotation.Log;
import com.vcit.common.core.controller.BaseController;
import com.vcit.common.core.domain.AjaxResult;
import com.vcit.common.enums.BusinessType;
import com.vcit.common.utils.poi.ExcelUtil;
import com.vcit.common.core.page.TableDataInfo;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import com.vcit.system.domain.vo.StudentGradeImportVo;

/**
 * 学生课程成绩Controller
 *
 * @author dff
 * @date 2025-02-11
 */
@RestController
@RequestMapping("/system/studentgrade")
@Api("学生成绩管理")
public class TStudentGradeController extends BaseController
{
    @Autowired
    private ITStudentGradeService tStudentGradeService;
    @Autowired
    private ITClazzStudentService clazzStudentService;
    @Autowired
    private ITClazzTermCourseService clazzTermCourseService;

    /**
     * 查询学生课程成绩列表
     */
    @PreAuthorize("@ss.hasPermi('system:studentgrade:list')")
    @GetMapping("/list")
    @ApiOperation("成绩信息列表")
    public TableDataInfo list(StudentGradeVo studentGrade)
    {
        startPage();
        List<StudentGradeVo> list = tStudentGradeService.selectTStudentGradeList(studentGrade);
        return getDataTable(list);
    }

    /**
     * 导出学生课程成绩列表
     */
    @ApiOperation("导出成绩信息列表")
    @PreAuthorize("@ss.hasPermi('system:studentgrade:export')")
    @Log(title = "学生课程成绩", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, StudentGradeVo studentGrade)
    {
        List<StudentGradeVo> list = tStudentGradeService.selectTStudentGradeList(studentGrade);
        ExcelUtil<StudentGradeVo> util = new ExcelUtil<StudentGradeVo>(StudentGradeVo.class);
        util.exportExcel(response, list, "学生课程成绩数据");
    }

    /**
     * 获取学生课程成绩详细信息
     */
    @ApiOperation("成绩详细信息")
    @PreAuthorize("@ss.hasPermi('system:studentgrade:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tStudentGradeService.selectStudentGradeVoById(id));
    }

    /**
     * 新增学生课程成绩
     */
    @ApiOperation("新增学生成绩信息")
    @PreAuthorize("@ss.hasPermi('system:studentgrade:add')")
    @Log(title = "学生课程成绩", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TStudentGrade tStudentGrade)
    {
        return toAjax(tStudentGradeService.insertTStudentGrade(tStudentGrade));
    }

    /**
     * 修改学生课程成绩
     */
    @ApiOperation("修改学生成绩信息")
    @PreAuthorize("@ss.hasPermi('system:studentgrade:edit')")
    @Log(title = "学生课程成绩", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TStudentGrade tStudentGrade)
    {
        return toAjax(tStudentGradeService.updateTStudentGrade(tStudentGrade));
    }

    /**
     * 删除学生课程成绩
     */
    @ApiOperation("删除学生成绩信息")
    @PreAuthorize("@ss.hasPermi('system:studentgrade:remove')")
    @Log(title = "学生课程成绩", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tStudentGradeService.deleteTStudentGradeByIds(ids));
    }

    /**
     * 按学生学号和学期ID查询该学生分配的课程列表
     */
    @ApiOperation("查询学生某学期分配课程列表")
    @GetMapping("/courses")
    public AjaxResult getStudentCoursesByTerm(@RequestParam String studentSno, @RequestParam Long termId) {
        // 1. 查找学生分班信息
        TClazzStudent query = new TClazzStudent();
        query.setSno(studentSno);
        List<TClazzStudent> clazzStudents = clazzStudentService.selectTClazzStudentList(query);
        if (clazzStudents == null || clazzStudents.isEmpty() || clazzStudents.get(0).getClazzId() == null) {
            return AjaxResult.error("未找到该学生的班级信息");
        }
        Long clazzId = clazzStudents.get(0).getClazzId();
        // 2. 查找班级-学期-课程三元关系
        TClazzTermCourse ctcQuery = new TClazzTermCourse();
        ctcQuery.setClazzId(clazzId);
        ctcQuery.setTermId(termId);
        List<TClazzTermCourse> courseList = clazzTermCourseService.selectListWithNames(ctcQuery);
        return AjaxResult.success(courseList);
    }

    /**
     * 导入学生成绩
     */
    @ApiOperation("导入学生成绩")
    @PostMapping("/import")
    public AjaxResult importStudentGrades(@RequestParam("file") MultipartFile file,boolean updateSupport) {
        return tStudentGradeService.importStudentGrades(file);
    }

    /**
     * 下载学生成绩导入模板
     */
    @ApiOperation("下载学生成绩导入模板")
    @PostMapping("/import/template")
    public void downloadImportTemplate(HttpServletResponse response) {
        ExcelUtil<StudentGradeImportVo> util = new ExcelUtil<>(StudentGradeImportVo.class);
        util.importTemplateExcel(response, "学生成绩导入模板");
    }
}
